﻿using FRMS.BUS;
using FRMS.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FRMS.Controllers
{
    /// <summary>
    /// ElevatorController
    /// </summary>
    [Route("api/[controller]")]
    [ApiExplorerSettings(GroupName = "v1")]
    [ApiController]
    [Authorize]
    public class ElevatorController : ControllerBase
    {
        private JwtSettings _jwtSettings;
        IHttpContextAccessor _access;
        public ElevatorController(IOptions<JwtSettings> jwtSettingsAccesser, IHttpContextAccessor access
        )
        {
            JwtSettings jwtSettings = new JwtSettings();
            jwtSettings.Audience = AppSettingsHelper.Configuration["JwtSettings:Audience"];
            jwtSettings.Issuer = AppSettingsHelper.Configuration["JwtSettings:Issuer"];
            jwtSettings.SecretKey = AppSettingsHelper.Configuration["JwtSettings:SecretKey"];
            _jwtSettings = jwtSettings;
            //_jwtSettings = jwtSettingsAccesser.Value;
            _access = access;
        }
        /// <summary>
        /// 新建电梯
        /// </summary>
        /// <param name="elevatorInfo"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("AddElevator")]
        public ResponseMessage AddElevator(ElevatorInfo elevatorInfo)
        {
            ResponseMessage message = new ResponseMessage();
            ElevatorBUS BUS = new ElevatorBUS();
            try
            {
                elevatorInfo.projectID = Convert.ToInt32(_access.HttpContext.Session.GetString("ProjectID"));
                message = BUS.AddElevator(elevatorInfo);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "新增电梯";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "新增电梯异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 修改电梯
        /// </summary>
        [HttpPost]
        [Route("EditElevator")]
        public ResponseMessage EditElevator(ElevatorInfo elevatorInfo)
        {
            ResponseMessage message = new ResponseMessage();
            ElevatorBUS BUS = new ElevatorBUS();
            try
            {
                message = BUS.EditElevator(elevatorInfo);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "修改电梯";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "修改电梯信息异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 删除电梯
        /// </summary>
        [HttpPost]
        [Route("DeleteElevator")]
        public ResponseMessage DeleteElevator(ElevatorInfo elevatorInfo)
        {
            ResponseMessage message = new ResponseMessage();
            ElevatorBUS BUS = new ElevatorBUS();
            try
            {
                message = BUS.DeleteElevator(elevatorInfo);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "删除电梯";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "删除电梯异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 查询电梯
        /// </summary>
        /// <param name="elevatorInfo"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("QueryElevator")]
        public ResponseMessage QueryElevator(ElevatorInfo elevatorInfo)
        {
            ResponseMessage message = new ResponseMessage();
            ElevatorBUS BUS = new ElevatorBUS();
            try
            {
                elevatorInfo.projectID = Convert.ToInt32(_access.HttpContext.Session.GetString("ProjectID"));
                message = BUS.QueryElevator(elevatorInfo);
            }
            catch(Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "查询所有电梯";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "查询所有电梯异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 电梯列表
        /// </summary>
        [HttpPost]
        [HttpGet]
        [Route("QueryElevatorList")]
        public ResponseMessage QueryElevatorList(ElevatorInfo elevatorInfo)
        {
            ResponseMessage message = new ResponseMessage();
            ElevatorBUS BUS = new ElevatorBUS();
            try
            {
                elevatorInfo.projectID = Convert.ToInt32(_access.HttpContext.Session.GetString("ProjectID"));
                message = BUS.QueryElevatorList(elevatorInfo);
            }
            catch(Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "查询电梯列表";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "查询电梯列表异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 载入电梯
        /// </summary>
        [HttpPost]
        [Route("LoadElevator")]
        public ResponseMessage LoadElevator(ElevatorInfo elevatorInfo)
        {
            ResponseMessage message = new ResponseMessage();
            ElevatorBUS BUS = new ElevatorBUS();
            try
            {
                message = BUS.LoadElevator(elevatorInfo);
            }
            catch(Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "载入电梯";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "查询电梯异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 切换模式
        /// </summary>
        [HttpPost]
        [Route("SwitchMode")]
        public ResponseMessage SwitchMode(ElevatorInfo elevatorInfo)
        {
            ResponseMessage message = new ResponseMessage();
            ElevatorBUS BUS = new ElevatorBUS();
            try
            {
                message = BUS.SwitchMode(elevatorInfo);
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "查询电梯异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }
    }
}
